package cn.wolfcode.wolf2w.redis.service.impl;

import ch.qos.logback.core.util.TimeUtil;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.redis.service.IUserinfoRedisService;
import cn.wolfcode.wolf2w.redis.util.Rediskeys;
import cn.wolfcode.wolf2w.util.Consts;
import cn.wolfcode.wolf2w.util.DateUtil;
import com.alibaba.fastjson.JSON;
import org.apache.tomcat.util.bcel.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;


@Service
public class UserinfoRedisServiceImpl implements IUserinfoRedisService {

    @Autowired
    private StringRedisTemplate template;

    @Override
    public void savePhonekey(String phone, String code) {
        String key = Rediskeys.VERIFY_CODE.join(phone);
        template.opsForValue().set(key, code, Rediskeys.VERIFY_CODE.getTime(), TimeUnit.SECONDS);
    }

    @Override
    public String getVerifyCode(String phone) {
        String key = Rediskeys.VERIFY_CODE.join(phone);
        return template.opsForValue().get(key);
    }

    @Override
    public String setToken(UserInfo userInfo) {
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        String key = Rediskeys.TOKEN_CODE.join(uuid);
        String value = JSON.toJSONString(userInfo);
        template.opsForValue().set(key, value, Rediskeys.TOKEN_CODE.getTime(), TimeUnit.SECONDS);
        return key;
    }

    @Override
    public UserInfo getToken(String token) {
        if (!StringUtils.hasLength(token)) {
            return null;
        }
        String s = template.opsForValue().get(token);
        UserInfo userInfo = null;
        if (template.hasKey(token)) {
            userInfo = JSON.parseObject(s, UserInfo.class);
            template.expire(token, Rediskeys.TOKEN_CODE.getTime(), TimeUnit.SECONDS);
        }
        return userInfo;
    }

    @Override
    public void setStatisVO(String key, StrategyStatisVORedisServiceImpl vo) {
        String token = Rediskeys.STRATEGY_STATIS_VO.join(key);
        template.opsForValue().set(token, JSON.toJSONString(vo));
    }

    @Override
    public List<UserInfo> findByCity(String keyword) {
        return null;
    }

    @Override
    public void setUserDayNum(String userid, String ownerId) {
        String join = Rediskeys.Users_DayNums_CODE.join(ownerId);
        Date date = new Date();
        Date endDate = DateUtil.getEndDate(date);
        long dateBetween = DateUtil.getDateBetween(date, endDate);
        if (!userid.equals(ownerId) ) {
            if (!template.hasKey(join)) {
                template.opsForValue().set(join, "1", dateBetween, TimeUnit.SECONDS);
            } else {
                template.opsForValue().increment(join);
            }
        }
    }

    @Override
    public String getUserDayNum(String ownerId) {
        String join = Rediskeys.Users_DayNums_CODE.join(ownerId);
        String s = template.opsForValue().get(join);
        return s;
    }

    @Override
    public void setUserAllNum(String userid, String ownerId) {
        String join = Rediskeys.Users_AllNums_CODE.join(ownerId);
        if (!userid.equals(ownerId)) {
            if (!template.hasKey(join)) {
                template.opsForValue().set(join, "1");
            } else {
                template.opsForValue().increment(join);
            }
        }
    }

    @Override
    public String getUserAllNum(String ownerId) {
        String join = Rediskeys.Users_AllNums_CODE.join(ownerId);
        String s = template.opsForValue().get(join);
        return s;
    }


}
